package cn.ecut.service.user;

import cn.ecut.dao.BaseDao;
import cn.ecut.dao.user.UserDao;
import cn.ecut.dao.user.UserDaoImpl;
import cn.ecut.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author ruanxin
 */
public class UserServiceImpl implements UserService {

    //业务层会调dao层，所以我们要引入dao层
    private UserDao userDao;

    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }

    @Override
    public User login(String userCode, String password) {
        Connection connection = null;
        User user = null;

        try {
            connection = BaseDao.getConnection();
            //通过业务层调用对应的具体的数据库操作
            user = userDao.getLoginUser(connection, userCode);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.close(connection, null, null);
        }

        return user;
    }

    @Override
    public boolean updateUserPassword(int id, String password) {
        boolean flag = false;
        Connection connection = null;

        //修改密码
        try {
            connection = BaseDao.getConnection();
            System.out.println("i just user service ");
            if (userDao.upateUserPassword(connection, id, password) > 0) {
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.close(connection, null, null);
        }

        return flag;
    }

    //查询记录数
    @Override
    public int getUserCount(String queryUserName, int queryUserRole) {
        int count = 0;
        Connection connection = null;
        System.out.println("UserServiceImpl queryUserName:  " + queryUserName);
        System.out.println("UserServiceImpl queryUserRole:  " + queryUserRole);

        try {
            connection = BaseDao.getConnection();
            count = userDao.getUserCount(connection, queryUserName, queryUserRole);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.close(connection, null, null);
        }
        return count;
    }

    @Override
    public ArrayList<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        ArrayList<User> userList = null;
        Connection connection=null;

        try {
            connection  = BaseDao.getConnection();
            System.out.println("queryUserName ---- > " + queryUserName);
            System.out.println("queryUserRole ---- > " + queryUserRole);
            System.out.println("currentPageNo ---- > " + currentPageNo);
            System.out.println("pageSize ---- > " + pageSize);
            userList= (ArrayList<User>) userDao.getUserList(connection, queryUserName, queryUserRole, currentPageNo, pageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.close(connection,null,null);
        }
        return userList;
    }


}
